//
//  SingleOption.m
//  JW_BasePorject
//
//  Created by JW on 2018/6/27.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "SingleOption.h"
#import <DTCoreText/DTCoreText.h>

NSString *htmls = @"<html><body>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cildo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum  Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</body></html>";

@interface SingleOption ()<DTAttributedTextContentViewDelegate>

@property (nonatomic, strong)UILabel *optionLabel;

@property (nonatomic, strong)DTAttributedLabel *contentLabel;

@end

@implementation SingleOption


- (void)setIsSingle:(BOOL)isSingle{
    _isSingle = isSingle;
    
}

- (void)setSelected:(BOOL)selected{
    
    
}


- (void)initConfigureView{
    self.optionLabel.text = @"A";
    self.optionLabel.backgroundColor = ZhuTiColor;
    self.optionLabel.frame = CGRectMake(5, 5, 30, 30);
    ViewBorderRadius(self.optionLabel, 15, 1, [UIColor lightGrayColor]);
    [self addSubview:self.optionLabel];
    
    [self addSubview:self.contentLabel];
    
    [self handleDTLabelDisplay];
}


- (void)handleDTLabelDisplay{
    NSData *data = [htmls dataUsingEncoding:NSUTF8StringEncoding];
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithHTMLData:data documentAttributes:NULL];
    self.contentLabel.attributedString = attributedString;
    
    DTCoreTextLayouter *layouter = [[DTCoreTextLayouter alloc] initWithAttributedString:attributedString];
    
    CGRect maxRect = CGRectMake(40, 5, self.frame.size.width - 40, CGFLOAT_HEIGHT_UNKNOWN);
    NSRange entireString = NSMakeRange(0, [attributedString length]);
    DTCoreTextLayoutFrame *layoutFrame = [layouter layoutFrameWithRect:maxRect range:entireString];
    
    CGSize sizeNeeded = [layoutFrame frame].size;
    
    
    self.contentLabel.frame = CGRectMake(40, 5, self.frame.size.width - 40, sizeNeeded.height);
    
    [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, sizeNeeded.height+10)];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initConfigureView];
    }
    return self;
}


- (DTAttributedLabel *)contentLabel{
    if (_contentLabel == nil) {
        _contentLabel = [[DTAttributedLabel alloc] initWithFrame:CGRectZero];
        _contentLabel.delegate = self;
    }
    return _contentLabel;
}

- (UILabel *)optionLabel{
    if (_optionLabel == nil) {
        _optionLabel = [[UILabel alloc] init];
        _optionLabel.textAlignment = NSTextAlignmentCenter;
        _optionLabel.font = [UIFont systemFontOfSize:16];
    }
    return _optionLabel;
}

@end
